package com.chanyachting.emall.action.newsactivity;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.form.NewsActivityForm;
import com.chanyachting.emall.service.NewsActivityService;
import com.chanyachting.emall.serviceImpl.NewsActivityServiceImpl;
import com.chanyachting.emall.util.RequestUtil;

public class QueryActivityAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		NewsActivityForm naForm = (NewsActivityForm) form;
		NewsActivityService naservice=new NewsActivityServiceImpl();
		
		try {
			
			RequestUtil ru=new RequestUtil(request);
			String id = ru.getParameter("channelId", "");
			Integer channelId = Integer.parseInt(id);
			String subChannelId = ru.getParameter("subchannelId", "");
			String strpageNo = ru.getParameter("pageNo","1");		//当前页数
			int pageNo = Integer.parseInt(strpageNo);
			int pageSize = 25;			//每页显示条数
			
			
			String ctype = request.getParameter("ctype");
			List listnews = null;
			if ("0".equals(ctype)) {				//查询主频道
				listnews = naservice.getNewsActivityByChannelId(channelId,pageNo,pageSize);
			} else {								//查询次频道
				Integer subchannelid = Integer.parseInt(subChannelId);
				listnews = naservice.getNewsActivityBySubChannelId(subchannelid,pageNo,pageSize);
			}
			
			String totals ="";						//返回总条数
			
			if ("0".equals(ctype)) {
				totals = String.valueOf(naservice.getAllNewsactivityById(ctype,id));	//主频道总条数
			} else {
				Integer subchannelid = Integer.parseInt(subChannelId);
				totals = String.valueOf(naservice.getAllNewsactivityById(ctype,subChannelId));
			}
			request.setAttribute("totals", totals);
			request.setAttribute("ctype", ctype);
			naForm.setList(listnews);
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("errors");
		}
		
		return mapping.findForward("newsActivityByChannel");
	}

}
